<template>
    <el-row class="edit">
        <el-container style="height: 100%;">
            <el-header height="60px">
                修改实体
            </el-header>
            <el-main>
                <el-form ref="editEntyData" :rules="rules" :model="editEntyData" label-width="130px"
                         label-position="left">
                    <el-form-item label="实体名称 :" prop="name">
                        <el-input v-model="editEntyData.name" placeholder="请输入属性名称"></el-input>
                    </el-form-item>
                    <el-form-item label="实体类型 :" prop="enType">
                        <el-select v-model="editEntyData.enType" filterable placeholder="请选择类型" style="width: 100%;" @change="changeValue">
                            <el-option
                                    v-for="( ky, val ) in enTypeOptions"
                                    :key="val"
                                    :label="ky"
                                    :value="val">
                                {{ky}}
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="关联设施名称：" prop="refName">
                        <el-input v-show="editEntyData.enType === 'Virtual'" v-model="editEntyData.refName" placeholder="请输入虚拟名称 "></el-input>
                        <el-select v-show="editEntyData.enType === 'Dev'" v-model="editEntyData.refName" filterable placeholder="请选择设备" style="width: 100%;" @change="changeSmt">
                            <el-option
                                    v-for="item in smtOptions"
                                    :key="item.uuid"
                                    :label="item.name"
                                    :value="item.name">
                                {{item.name}}
                            </el-option>
                        </el-select>
                        <comtree v-show="editEntyData.enType === 'Yard'" @treeSelected = "treeSelected" v-model="editEntyData.refName" ref="comtree"></comtree>
                    </el-form-item>
                </el-form>
            </el-main>
            <el-footer height="80px">
                <el-row style="height: 20px;margin-bottom: 5px;">
                    <transition name="el-zoom-in-bottom">
                        <el-row v-show="infoShow" style="display: flex;justify-content: center;align-items: center;">
                            {{ (info !== '') ? "提示信息："+info : ''}}
                        </el-row>
                    </transition>
                </el-row>

                <el-row style="display: flex;justify-content: center;align-items: center;">
                    <el-button @click="submit" type="primary">
                        保存
                    </el-button>
                    <el-button @click="editOut">
                        取消
                    </el-button>
                </el-row>
            </el-footer>
        </el-container>
    </el-row>
</template>

<script>
    import {replaceSpacesObj} from '../../../utils/webUtils.js';
    import comtree from '../../public/comtree.vue'

    export default {
        components: {comtree},
        name: "editEnty",
        props: {
            entyUuid: {
                type: String
            }
        },
        data() {
            return {
                formAlignLeft: 'left',
                editEntyData: {
                    uuid: this.entyUuid,
                    name: '',
                    enType:'',
                    refUuid:'',
                    refName:''
                },
                enTypeOptions: {},
                smtOptions:[],
                rules: {
                    name: [
                        {required: true, message: '事件实体名称不能为空', trigger: 'blur'},
                    ]
                },
                info: '',
                infoShow: false
            }
        },
        //深度监听父组件的props传值
        watch: {
            entyUuid(val) {
                this.editEntyData.uuid = val;
            }
        },
        mounted() {
            this.queryEnty();
            this.queryEnType();
            this.querySmt();
            this.$refs.comtree.autoWidth = '200'
            this.$refs.comtree.autoHeight = '150'
        },
        methods: {
            changeSmt(){
                this.smtOptions.forEach( list =>{
                    if(list.name === this.editEntyData.refName){
                        this.editEntyData.refName = list.name
                        this.editEntyData.refUuid = list.uuid
                    }
                })
            },
            // 获取当前树节点所有信息
            treeSelected (treeNode) {
                this.editEntyData.refName = treeNode.name
                this.editEntyData.refUuid = treeNode.id
            },
            //获取设备所有数据
            querySmt() {
                this.axios.request({
                    method: "get",
                    url: "./smt/kdi/dev/search/findAll",
                    params: {}
                }).then(response => {
                    this.smtOptions = response.data.content;
                }).catch(error => {
                    console.log(error);
                });
            },
            changeValue(){
                this.editEntyData.refName = '';
                this.$refs.comtree.inputValue = '';

            },
            //查询后端事件类型标识枚举
            queryEnType() {
                this.axios
                    .request({
                        method: "get",
                        url: "./smt/kdi/enums/EnType",
                        params: {}
                    })
                    .then(response => {
                        this.enTypeOptions = response.data;
                    })
                    .catch(error => {
                        console.log(error);
                    });
            },
            //通过uuid查询当前对象信息
            queryEnty() {
                this.axios.request({
                    method: "get",
                    url: "./smt/kdi/enty/" + this.editEntyData.uuid,
                    headers: {},
                    params: {}
                }).then(response => {
                    this.insertEnty(response.data);
                }).catch(error => {
                    console.log(error);
                });
            },
            insertEnty(editEntyData) {
                this.editEntyData.name = editEntyData.name;
                this.editEntyData.enType = editEntyData.enType;
                this.editEntyData.refUuid = editEntyData.refUuid;
                this.editEntyData.refName = editEntyData.refName;
                this.$refs.comtree.inputValue = editEntyData.refName;
            },
            submit() {
                this.$refs['editEntyData'].validate((valid) => {
                    if (!valid) {
                        return;
                    }
                    this.axios.request({
                        method: "put",
                        url: "./smt/kdi/enty/" + this.editEntyData.uuid,
                        headers: {
                            "Content-Type": "application/json;charset=UTF-8"
                        },
                        data: replaceSpacesObj(this.editEntyData)
                    }).then(() => {
                        this.info = '修改成功,两秒后关闭';
                        this.infoShow = true;
                        setTimeout(() => {
                            this.infoShow = false;
                            this.$emit("editState", false);
                            this.info = '';
                        }, 2000);
                    }).catch(error => {
                        console.log(error);
                        this.info = "修改失败";
                        this.infoShow = true;
                    });

                });
            },
            cleanEditAttr() {
                this.editEntyData = {
                    name: '',
                    enType:'',
                    refUuid:'',
                    refName:''
                };
            },
            editOut() {
                this.infoShow = false;
                this.cleanEditAttr();
                this.info = '';
                this.$emit("editState", false);
            }
        }
    }
</script>

<style scoped>
    .edit {
        width: 360px;
        height: 100%;
        z-index: 100;
    }


</style>